<?php
/**
 * Material Customer
 */

use Fox\MaterialManagement\Material\Bom\Link;
use Fox\MaterialManagement\Material\Hierarchy;
use Fox\MaterialManagement\Material;
use Fox\Html\TabPanel\Tab,Fox\Html\Wrapper,Fox\Html\TabPanel,Fox\Html\Screen;

$hierarchyValue = $selectedNode = null;
$id = Xview::instance()->val("materialid");
try {
	$object = new Material($id);
	$data = $object->getData();
	$data['materialid'] = $object->getId();
	$data['tComponents'] = $object->bom();
	$hierarchy = $data['hierarchy'];

	for($level = 1;$level < 10;$level++)
	{
		$code = substr($hierarchy, 0,2);
		if(empty($code))
			break;
		$selectedNode[$level][$code] = true;
		$hierarchy = substr($hierarchy,2);
	}
}
catch(Fox\Object\Exception\NotExist $e)
{
	Xview::instance()->systemError();
}
if(!$object->exists())
{
	Xview::instance()->systemError();
}

$screen = new Screen("eMaterial","update");
$tabPanel = new TabPanel("pMaterial");
$wMasterData = new Wrapper("wMasterData");
$blHeader = Xview::instance()->component("material", "header");
$wMasterData->add($blHeader);
// Hierarchy Part
$blHierarchy = Xview::instance()->component("material", "hierarchy");
if($screen->submitted())
{
	$hierarchyTree = $blHierarchy->item("hierarchyTree")->get(null,true);

	foreach($hierarchyTree as $node)
	{
		if(!isset($node['coche'])) {
			continue;
		}
		if($node['coche'] === 't' || $node['coche'] === 1)
		{
			$selectedNode[$node['level']][$node['code']] = 1;
			if(isset($levels[$node['level']]))
			{
				Fox\Message::Error(Msg(Material::BUNLDE_NAME,20));
			}
			$levels[$node['level']] = true;
			$hierarchyValue .= $node['code'];
		}
	}
}
$root = new Fox\Node(Fox\Node::ROOT_NODE);
$hiearchies = Hierarchy::loadTree();
foreach($hiearchies as $hierarchy)
{
	$parentid = $hierarchy['parentid'];
	if(!empty($parentid) && !isset($hiearchyTree[$parentid]))
		continue;
	if(isset($selectedNode[$hierarchy['level']][$hierarchy['code']]))
		$hierarchy['coche'] = 't';
	else
		$hierarchy['coche'] = 'f';
	if(empty($parentid))
		$hiearchyTree[$hierarchy['id']] = $root->add(new Fox\Node($hierarchy));
	else
		$hiearchyTree[$hierarchy['id']] = $hiearchyTree[$parentid]->add(new Fox\Node($hierarchy));
}
$blHierarchy->item("hierarchyTree")->initialize($root,true);
$wMasterData->initialize($data);
$wMasterData->add($blHierarchy);
$tabPanel->add(new Tab("tMasterData",Msg(Fox\Msg::BUNDLE,Fox\Msg::TITLE_MASTER_DATA),$wMasterData));
// Component Tab
$bComponents = Xview::instance()->component("material", "components");
$bComponents->initialize($data);
$tabPanel->add(new Tab("tComponents",Msg(Material::BUNLDE_NAME,16),$bComponents));
$screen->add($tabPanel);


if($screen->submitted())
{
	$inputs = $screen->get();
	//
	if(!$screen->errors())
	{
		try {
			$root = new Fox\Node(Fox\Node::ROOT_NODE);
			$id = $object->getId();
			// Convert Hiearchy into a string
			$inputs['hierarchy'] = $hierarchyValue;

			$components = $object->components();
			// Manage Partners
			foreach($inputs["tComponents"] as $key => $componentData)
			{
				if(!isset($componentData['materialid']))
					continue;
				if($componentData['materialid'] == $id)
				{
					if(isset($components[$componentData['componentid']]))
					{
						// Existing Component
						unset($components[$componentData['componentid']]);
					}
					else
					{
						// New Component
						$component = new Link();
						$component->setFields($componentData);
						$object->component($component);
					}
					$root->add(new Fox\Node($componentData));
				}
				else
				{

				}
			}

			$bComponents->item("tComponents")->initialize($root,true);
			foreach($components as $component)
			{
				$component->delete();
			}

			// Update Customer
			$object->setFields($inputs);
			$object->prepareUpdate($object::CHANGE_DEFAULT);
			$object->update();
			Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,"<b>".$object->label."</b>"));
		//	Xview::instance()->redirect("?mod=material&act=update&materialid=$id");
		}
		catch(Fox\Object\Exception\Save $e)
		{
			var_dump($e);
			Fox\Core\Errors::pushMessage($e);
		}
		catch(Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}
}
echo '<h1>'.Msg(Material::BUNLDE_NAME,18).'</h1>';
echo $screen->Display();